/**
 * @(#) TrainingMediator.as
 *
 * @date		2009. 04. 03.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	교육 신청 - Mediator
 * 				교육을 조회하고 신청하는 Presentation Logic을 관리 
 */
package com.skt.common_hr.training.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.approval.RequestApprovalMediator;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.training.TrainingFacade;
	import com.skt.common_hr.training.events.SelCourseEvent;
	import com.skt.common_hr.training.model.ReqTrainingProxy;
	import com.skt.common_hr.training.model.TrainingApprovalProxy;
	import com.skt.common_hr.training.model.TrainingProxy;
	import com.skt.common_hr.training.model.vo.TrainingDetailVO;
	import com.skt.common_hr.training.view.components.ReqTraining;
	import com.wcl.utils.StringUtil;
	
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	

	public class ReqTrainingMediator extends CommonMediator
	{
		public static const NAME:String = "ReqTrainingMediator"; 
		
		private var _currentKey:String;
				
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function ReqTrainingMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		override protected function initializeView():void
 		{
			onInitialize();
			setEventListener();
			loadCourseList();
 		}
 		
 		private function onInitialize():void{
 			view.courseTree.selectedIndex = -1;
 			clearDetailView();
 		}
 		
 		private function setEventListener():void
 		{
			view.addEventListener(SelCourseEvent.SELECTED_COURSE, onSelectedTree);
			view.addEventListener(ReqTraining.SAVE_TRAINING, onSaveTraining);
			view.addEventListener(ReqTraining.CANCLE_REQ_TRAINING, onCancleReqTraining);
 		}
 		
 		private function removeEventListener():void
 		{
			view.removeEventListener(SelCourseEvent.SELECTED_COURSE, onSelectedTree);
			view.removeEventListener(ReqTraining.SAVE_TRAINING, onSaveTraining);
			view.removeEventListener(ReqTraining.CANCLE_REQ_TRAINING, onCancleReqTraining);
 		}

		public function execute():void
 		{
 			onInitialize();
 			bActivated = true;
 			commonFacade.setStatusMessage("STATUS_START_REQUEST");
 		}
 		
 		private function loadCourseList():void
 		{
 			var proxy:TrainingProxy = facade.retrieveProxy(TrainingProxy.NAME) as TrainingProxy;
			proxy.loadTrainingCourse();
 		}

		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					TrainingFacade.LOAD_TRAINING_COURSE_COMPLETE,
					TrainingFacade.LOAD_TRAINING_COURSE_FAILED,
					TrainingFacade.LOAD_DETAIL_COMPLETE,
					TrainingFacade.LOAD_DETAIL_FAILED,
					TrainingFacade.SAVE_REQ_TRAINING_COMPLETE,
					TrainingFacade.SAVE_REQ_TRAINING_FAILED,
					TrainingFacade.DELETE_REQ_TRAINING_COMPLETE,
					TrainingFacade.DELETE_REQ_TRAINING_FAILED,
					RequestApprovalMediator.APPROVAL_CANCEL,
					RequestApprovalMediator.APPROVAL_COMPLETE
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case TrainingFacade.LOAD_TRAINING_COURSE_COMPLETE:				// 교육 일정 조회 성공
					view.listDP = TrainingProxy(note.getBody()).trainingList;
					view.treeDP = TrainingProxy(note.getBody()).trainingTree;
					break; 		
				case TrainingFacade.LOAD_TRAINING_COURSE_FAILED:				// 교육 일정 조회 실패
					trace("LOAD_TRAINING_COURSE_FAILED");
					break; 		
				case TrainingFacade.LOAD_DETAIL_COMPLETE:						// 상세 교육일정 조회 성공
					setDetailView(note.getBody() as TrainingProxy);
					break;
				case TrainingFacade.LOAD_DETAIL_FAILED:							// 상세 교육일정 조회  실패
					CommonAlert.showMessage(commonFacade.bundle, (note.getBody() as ReqTrainingProxy).faultMessage);
					break;	
				case TrainingFacade.SAVE_REQ_TRAINING_COMPLETE:					// 교육 신청 저장 성공
					_currentKey = ReqTrainingProxy(note.getBody()).resultKey 
					execApproval(_currentKey);
					break;
				case TrainingFacade.SAVE_REQ_TRAINING_FAILED:					// 교육 신청 저장 실패
					CommonAlert.showMessage(commonFacade.bundle, (note.getBody() as ReqTrainingProxy).faultMessage);
					break;		
				case RequestApprovalMediator.APPROVAL_COMPLETE:					// 결재 요청  성공
					bActivated = false;
					facade.sendNotification(TrainingFacade.SUCCEED_REQ_TRAINING, this);
					break;
				case RequestApprovalMediator.APPROVAL_CANCEL:					// 결재 요청 취소
					cancelApproval(_currentKey);
					break;
				case TrainingFacade.DELETE_REQ_TRAINING_COMPLETE:				// 결재 요청 취소시 저장된 데이터를 삭제함
					commonFacade.setStatusMessage("STATUS_DELETED_TEMPDATA");
					break;
				case TrainingFacade.DELETE_REQ_TRAINING_FAILED:					// 데이터 삭제 실패
					// 그냥 아무 동작하지 않고 지나침
					trace("임시 저장된 데이터 삭제 실패");
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
		
 
 		private function setDetailView(proxy:TrainingProxy):void
 		{
			view.trainingDetail 		= proxy.trainingDetail;
			view.trainingTarget 		= proxy.trainingTarget;
			view.trainingPurpose 		= proxy.trainingPurpose;
			view.trainingMainContents 	= proxy.trainingMainContents;
			view.trainingLeave 			= proxy.trainingLeave;
 		}
 
 		private function clearDetailView():void
 		{
 			view.clearDetailView();
 		}
 
 
 		private function execApproval(key:String):void
 		{
 			var approval:RequestApprovalMediator = facade.retrieveMediator(RequestApprovalMediator.NAME) as RequestApprovalMediator;
 			if(approval==null) {
 				approval = new RequestApprovalMediator(view, "07");
 				facade.registerMediator(approval);
 			}
 			
 			var appProxy:TrainingApprovalProxy = facade.retrieveProxy(TrainingApprovalProxy.NAME) as TrainingApprovalProxy;
 			appProxy.key = key;
 			appProxy.dataProvider = buildRequestParam();
 			approval.execRequest(view, appProxy);
 		}
 		
 		private function cancelApproval(key:String):void
 		{
 			var proxy:ReqTrainingProxy = facade.retrieveProxy(ReqTrainingProxy.NAME) as ReqTrainingProxy;
 			proxy.deleteTraining([{GWAREKEY: key}]);
 		}
 		
 
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
  		private function onSelectedTree(event:SelCourseEvent):void{
 			trace("onSelectedTree");
 			SelectedCourseCall(event.courseCode, event.startDt, event.endDt);
 		}
 		
 		private function SelectedCourseCall(code:String, sdt:Date, edt:Date):void{
 			var proxy:TrainingProxy = facade.retrieveProxy(TrainingProxy.NAME) as TrainingProxy;
			proxy.loadDetail(code, sdt, edt);
		}
		
		private function onCancleReqTraining(event:Event):void{
			bActivated = false;
			facade.sendNotification(TrainingFacade.CANCLE_REQ_TRAINING, false);
		}
		
		//신청
		private function onSaveTraining(envet:Event):void{
			var proxy:ReqTrainingProxy = facade.retrieveProxy(ReqTrainingProxy.NAME) as ReqTrainingProxy;
	 		
	 		var paramTable:Array = new Array();
	 		paramTable.push(buildRequestParam());
	 		proxy.save(paramTable);
		}
		
		
		private function buildRequestParam():TrainingDetailVO
		{
	 		var vo:TrainingDetailVO = new TrainingDetailVO();
	 		vo.PERNR = StringUtil.trimFront(Variables.myPernr);
	 		vo.DCODE = view.trainingDetail.D_OBJID;
	 		vo.BEGDA = view.trainingDetail.BEGDA;
	 		vo.ENDDA = view.trainingDetail.ENDDA;

	 		vo.L_STEXT	= view.trainingDetail.L_STEXT;
	 		vo.D_STEXT	= view.trainingDetail.D_STEXT;
	 		vo.TXTSING	= view.trainingDetail.TXTSING;
	 		vo.U_STEXT	= view.trainingDetail.U_STEXT;
	 		vo.R_STEXT	= view.trainingDetail.R_STEXT;
	 		vo.IKOSTT	= view.trainingDetail.IKOSTT;
	 		vo.NHOURS	= view.trainingDetail.NHOURS;
	 		vo.NDAYS	= view.trainingDetail.NDAYS;
	 		return vo;
		}
  		
 



		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():ReqTraining
        {
            return viewComponent as ReqTraining;
        }
		
		
	}
}